package com.ww.springboot.boot.algorithm.string;

import java.util.Stack;

/**
 * 栈 -》括号
 *
 * @author wanwei
 * @since 2021-04-03 14:07
 */
public class BracketsDemo {


    public static void main(String[] args) {
        System.out.println(method("(){}}{"));
    }

    private static Boolean method(String s) {
        Stack<Character> characters = new Stack<>();
        for (int i = 0; i < s.length(); i++) {
            char ch = s.charAt(i);
            if (ch == '(') {
                characters.push(')');
            } else if (ch == '{') {
                characters.push('}');
            } else if (ch == '[') {
                characters.push(']');
            } else {
                if(characters.empty() || characters.pop() != ch){
                    return false;
                }
            }
        }
        return characters.empty();
    }

    private static Boolean method2(String s) {
        int length = s.length() / 2;
        for (int i = 0; i < length; i++) {
            s = s.replace("()", "").replace("{}", "").replace("[]", "");
        }

        return s.length() == 0;
    }
}
